
import Request from '@/api/http/luch-request/index.js' // 下载的插件

import {
	baseUrl,
	mchId,
	mchKey,
	payUrl
} from './setting'
import {getStoreSyncUtil,getNowTime} from "@/utils/store.js"
let Base64 = require('@/utils/base64.min.js').Base64;
import vm from "@/main.js"
let url = "/api";
//#ifdef MP-WEIXIN 
url = baseUrl;
//#endif

let loginUser = {};
let http = new Request({
	baseURL: url,
	method: 'POST',
	custom: {
		loadingText: "请求中",
		isShowErrTip: true
	},

	dataType: 'json',
	timeout: 60000,
});

http.interceptors.request.use((req) => {
	console.log("请求", req);
	if (req.url == '/blade-auth/oauth/token' || req.url == '/iot-device-saas-customer/customer/checkRegister' ||
		req.url == '/iot-device-saas-customer/customer/getCustomerWeixinPhone' || req.url ==
		'/iot-device-saas-customer/customer/register' || req.url == payUrl+'/wellpay/orderapi/preorder') {
			if(req.url == payUrl+'/wellpay/orderapi/preorder'){
				let authorization = Base64.encode(mchId + ":" + mchKey);
				req.header['authorization'] = authorization;
				req.header['mchApp'] = 'weixin_xcx';
				req.header['reqTime'] = getNowTime();
			}
		//req.header['Tenant-Id'] = '000000';
	} else {
		if (vm.$validatenull(loginUser)) {
			//#ifdef MP-WEIXIN 
			loginUser = vm.$store.state.loginUser;
			//#endif
			//#ifdef H5
			loginUser = getStoreSyncUtil("loginUser");
			//#endif
		}

		req.header['Blade-Auth'] = 'bearer ' + loginUser.access_token;
	}
	// 客户端认证参数
	if(req.url != payUrl+'/wellpay/orderapi/preorder'){
		req.header['Authorization'] = 'Basic c2FiZXI6c2FiZXJfc2VjcmV0';
	}

	if (!vm.$validatenull(req.custom.loadingText)) {
		uni.showLoading({
			title: req.custom.loadingText,
			mask: true
		})
	}
	return req;
}, req => {
	return Promise.reject(req);
})

let orginData = ['/blade-auth/oauth/token'];
http.interceptors.response.use((response) => {
	console.log("响应", response);
	let loadingText = response.config.custom.loadingText;
	if (!vm.$validatenull(loadingText)) {
		uni.hideLoading();
	}
	let data = response.data;
	if (orginData.includes(response.config.url)) {
		return [null, data];
	}
	if(response.config.url == payUrl+'/wellpay/orderapi/preorder'){
		if(data.retCode=="0000"){
			if (response.config.custom.isShowSuccessText) {
				vm.$showToast(response.config.custom.isShowSuccessText, "success");
			}
			return [null, data.data];
		}else{
			if (response.config.custom.isShowErrTip) {
				vm.$showModal(data.retMsg);
				throw new Error(data.retMsg);
			}
			//业务逻辑错误
			return [data.code, null];
		}
	}else{
		if(response.config.url == "https://apis.map.qq.com/tracks/trace/search" || response.config.url == "https://restapi.amap.com/v4/grasproad/driving"){
			return [null, data];
		}else{
			if (data.code == "200") {
				if (response.config.custom.isShowSuccessText) {
					vm.$showToast(response.config.custom.isShowSuccessText, "success");
				}
				return [null, data.data];
			} else {
				if (response.config.custom.isShowErrTip) {
					vm.$showModal(data.msg);
					throw new Error(data.msg);
				}
				//业务逻辑错误
				return [data.code, null];
			
			}
		}
		
	}
	
}, (response) => {
	console.log("错误响应", response);
	/*  对响应错误做点什么 （statusCode !== 200）*/ //请求错误
	uni.hideLoading();
	if (response.statusCode == "401") {
		vm.$showModal(response.data.msg ? response.data.msg + ',跳转到首页' : "请求超时,请稍后再试", () => {
			vm.$reLaunch("/pages/views/home/index")
		});
	} else {
		vm.$showModal(response.data.msg ? response.data.msg : "请求超时,请稍后再试");
	}

	throw new Error(response.data.msg ? response.data.msg : "请求超时,请稍后再试");
	return [response.data.msg ? response.data.msg : "error", null];
})

let requestUtil = (url, method, params, loadingText = "请求中", isShowSuccessText = '', isShowErrTip = true,
	header = {},isHasParamsData = false,data) => {
	let paramName = method == "get" ? "params" : "data";
	if(!isHasParamsData){
		return http.request({
			url: `${url}`,
			method: method,
			params: params,
			data: params,
			header: header,
			custom: {
				loadingText,
				isShowSuccessText,
				isShowErrTip
			},
		})
	}else{
		return http.request({
			url: `${url}`,
			method: method,
			params: params,
			data: data,
			header: header,
			custom: {
				loadingText,
				isShowSuccessText,
				isShowErrTip
			},
		})
	}
	
	
	
}



module.exports = requestUtil;
